package org.darkforest.merchant.ajaxTemplete.impl;

import org.darkforest.common.util.SystemConstant;
import org.darkforest.merchant.ajaxTemplete.AlbumAjaxPage;
import org.darkforest.model.AlbumClass;
import org.darkforest.model.AlbumPic;
import org.darkforest.model.AlbumPicPageBean;

import java.util.List;

/**
 * Created by valentina on 15/3/19.
 * 公用模板类
 */
public abstract class AlbumAjaxPageTemplete implements AlbumAjaxPage {

    protected String insertType;
    protected String basePath = "";
    protected String aliyun_path = SystemConstant.ALIOSS_PATH;
    protected Integer specSize = 0;

    protected String getAjaxUrl(){
        return basePath+"/album/ajaxPicList?insertType="+insertType+"&specSize="+specSize;
    }

    /**
     * 获取默认头部div代码
     * @return
     */
    protected String getMainDiv(Integer specSize) {
        String tpl = "<div class='goods-gallery add-step2'><a class='sample_demo' id='select_submit_"+insertType+"'  style='display:none;'>提交</a>";
        return tpl;
    }

    /**
     * 获取相册列表代码
     * @param albumId
     * @return
     */
    protected String getAlbumSelectCode(List<AlbumClass> albumList,Integer albumId) {
//        List<AlbumClass> albumList = albumClassService.getAlbumClassList(storeId);
        String tpl = "<div class='nav'><span class='l'>用户相册 >全部图片</span><span class='r'>" +
                "           <select name='jumpMenu' id='jumpMenu_"+insertType+"' style='width:100px;'>"+
                "               <option style='width:80px;' value=''>请选择...</option>";
        //循环出相册信息
        String selected ="";
        for(AlbumClass album : albumList) {
            if(albumId!=null && album.getAclassId()==albumId){
                selected = "selected";
                tpl = tpl.replace("全部图片",album.getAclassName());
            }else{
                selected = "";
            }
            tpl +=   "          <option value='"+ album.getAclassId() +"' "+selected+" style='width:80px;'>"+album.getAclassName()+"</option>";
        }
        tpl +=  "           </select>" +
                "         </span></div>" ;
        return tpl;
    }

    /**
     * 获取循环出来的图片列表代码
     * @param picPage
     * @return
     */
    protected String getPicListCode(AlbumPicPageBean picPage,Integer specSize) {
        String tpl ="";
        if(picPage.getResultList()!=null && picPage.getResultList().size()>0){
            tpl +=  "<ul class='list'>";
            //循环出图片信息 js需要用双引号否则不执行
            for (AlbumPic pic:picPage.getResultList()){
                tpl +=   "      <li onclick=\"insert_"+insertType+"(";
                //主图以及移动端插入360px的缩略图
//                if("img".equals(insertType)||"mb_img".equals(insertType)){
//                    tpl +=   "'"+pic.getApicCover()+"',";
//                }
                tpl +=   "'"+pic.getApicCover()+"',";
                tpl +=   "'"+aliyun_path+"/360_"+pic.getApicCover()+"');\">" ;
                tpl +=   "           <a href='JavaScript:void(0);'><img src='"+aliyun_path+"/60_"+pic.getApicCover()+"' title='"+pic.getApicName()+"'/></a></li>" ;
            }
            tpl +=  "</ul>";
        }else{
            tpl += "<div class='warning-option'><i class='icon-warning-sign'></i><span>相册中暂无图片</span></div>";
        }
        return tpl;
    }

    /**
     * 获取分页代码
     * @param picPage
     * @return
     */
    protected String getPageBarCode(AlbumPicPageBean picPage) {
        String tpl ="";
        String albumInfo = picPage.getAlbumId()==null?"":"&albumId="+picPage.getAlbumId();
        //分页列表
        tpl +=  "         <div class='pagination'><ul>";
        if(picPage.getStart()==1){
            tpl+= "         <li><span>首页</span></li>" +
                    "         <li><span>上一页</span></li>" ;
        }else {
            tpl+= "         <li><a class='demo' href='"+getAjaxUrl()+albumInfo+"&page=1'><span>首页</span></a></li>" +
                    "         <li><a class='demo' href='"+getAjaxUrl()+albumInfo+"&page="+(picPage.getStart()-1)+"'><span>上一页</span></a></li>" ;
        }

        if (picPage.getPageCount()>1){
            for(int p =1;p<=picPage.getPageCount();p++) {
                if(picPage.getStart()==p){
                    tpl += "    <li><span class='currentpage'>" + p + "</span></li>";
                }else{
                    tpl += "    <li><a class='demo' href='" + getAjaxUrl() + albumInfo + "&page=" + p + "'><span>" + p + "</span></a></li>";
                }
            }
        }else {
            tpl += "        <li><span class='currentpage'>1</span></li>";
        }

        if (picPage.getStart()<picPage.getPageCount()){
            tpl +=  "       <li><a class='demo' href='"+ getAjaxUrl() + albumInfo +"&page="+(picPage.getStart()+1)+"'><span>下一页</span></a></li>" +
                    "       <li><a class='demo' href='"+ getAjaxUrl() + albumInfo +"&page="+picPage.getPageCount()+"'><span>末页</span></a></li>";
        }else{
            tpl +=  "       <li><span>下一页</span></li>" +
                    "       <li><span>末页</span></li>" ;
        }

        tpl +=  "         </ul></div>" +
                "     </div>";
        return tpl;
    }

    /**
     * 获取javascript代码
     * @return
     */
    protected String getScriptCode() {
        return null;
    }

    /**
     * 返回完整的ajax相册页面代码
     * @param picPage
     * @return
     */
    public String getAlbumCode(String basePath,List<AlbumClass> albumList,AlbumPicPageBean picPage,Integer specSize) {
        this.basePath = basePath;
        this.specSize = specSize;
        return this.getMainDiv(specSize)+this.getAlbumSelectCode(albumList,picPage.getAlbumId())
                +this.getPicListCode(picPage,specSize)+this.getPageBarCode(picPage)+this.getScriptCode();
    }
}
